<template>
  <!-- 导航栏 -->
  <van-nav-bar title="订单填写" left-arrow @click-left="$router.back()" />

  <!-- 主体内容 -->
  <div class="content">
    <!-- 酒店信息 -->
    <van-cell-group inset>
      <van-image width="300" height="100" :src="form.psice" />
      <van-cell title="深圳光明希尔顿欢朋酒店" :label="form.roomType" />

      <van-cell title="限量优惠">
        <template #label>
          <div class="discount">赠送两份自助早餐</div>
          <div class="discount">可累积希尔顿荣誉客会积分及房晚</div>
          <van-tag type="danger">不可取消</van-tag>
        </template>
      </van-cell>
    </van-cell-group>

    <!-- 日期信息 -->
    <van-cell-group inset>
      <van-cell title="周四入住" :value="form.start" />
      <van-cell title="周五离店" :value="form.end" />
      <van-cell title="房间数">
        <van-stepper v-model="form.roomNum" min="1" max="10" @click="getroomtype" />
      </van-cell>
    </van-cell-group>

    <!-- 预订信息 -->
    <van-cell-group inset>
      <van-field v-model="form.bookingPhone" label="预订手机号" placeholder="请输入手机号">
        <template #button>
          <van-button size="small" type="primary" @click="getPhone">一键获取</van-button>
        </template>
      </van-field>

      <van-field v-model="form.bookingName" label="预订人姓名" placeholder="请填写预订人姓名" />

      <van-field v-model="form.roomName" label="入住人" placeholder="入住人姓名，1间房填1人" />

      <van-field v-model="form.phone" label="联系电话" placeholder="请填写您的联系电话" />

      <van-cell title="性别">
        <van-radio-group v-model="form.roomNameSex" direction="horizontal">
          <van-radio :name="true">男</van-radio>
          <van-radio :name="false">女</van-radio>
        </van-radio-group>
      </van-cell>
    </van-cell-group>

    <!-- 温馨提示 -->
    <div class="tip">温馨提示：20分钟内未完成支付，订单会自动取消</div>
  </div>

  <!-- 底部提交栏 -->
  <div class="footer">
    <div class="price">￥{{ form.amount }}</div>
    <van-button round type="primary" block @click="AlipayPay">提交订单</van-button>
  </div>
</template>

<script setup>
import { onMounted, ref } from "vue";
import {
  NavBar as VanNavBar,
  CellGroup as VanCellGroup,
  Cell as VanCell,
  Tag as VanTag,
  Stepper as VanStepper,
  Field as VanField,
  RadioGroup as VanRadioGroup,
  Radio as VanRadio,
  Button as VanButton,
} from "vant";
import axios from "axios";
import { useRoute } from "vue-router";
import { AlipayPagePay } from "@/api/AlipaySandbox/AlipayIndex";
import SnowflakeId from "snowflake-id";
import { getroomtypes, InsertReservations } from "@/api/Index";
import router from "@/router";
const route = useRoute();

const form = ref({
  orderNumber: "",
  roomType: "",
  startTime: "",
  endTime: "",
  roomNum: "",
  bookingPhone: "",
  bookingName: "",
  roomName: "",
  phone: "",
  roomNameSex: true,
  amount: null,
  psice: "",
});
const getPhone = () => {
  // 实现获取手机号逻辑
  form.value.bookingPhone = "13843961234";
};
const snowflake = new SnowflakeId();
const getroomtype = () => {
  getroomtypes(route.query.id).then((res) => {
    form.value.roomType = res.data.roomTypeName;
    form.value.amount = res.data.roomTypePrice * route.query.diff * form.value.roomNum;
    form.value.startTime = route.query.checkInDate;
    form.value.endTime = route.query.checkOutDate;
    form.value.orderNumber = snowflake.generate();
    form.value.psice = res.data.roomTypeImg;
    console.log(form.value.psice);
  });
};

onMounted(() => {
  getroomtype();
});

//支付宝支付
const AlipayPay = () => {
  debugger;
  InsertReservations(form.value).then((res) => {
    if (res.code == 200) {
      router.push({ path: "/RoomType" });
    }

  });
  // const AlipayTradePagePayViewModel = {
  //   OutTradeNo: form.value.orderNumber,
  //   Subject: "订房订单",
  //   Body: "请支付订单金额",
  //   TotalAmount: form.value.Amount,
  //   NotifyUrl: "http://118.178.139.160:8067/RoomType",
  //   ReturnUrl: "http://118.178.139.160:8067/RoomType",
  // };

  // AlipayPagePay(AlipayTradePagePayViewModel, form.value).then((res) => {
  //   document.write(res);
  // });
};
</script>

<style scoped>
.booking-page {
  background: #f5f5f5;
  min-height: 100vh;
}

.content {
  padding-bottom: 80px;
}

.discount {
  color: #ff4444;
  font-size: 12px;
  margin: 4px 0;
}

.tip {
  padding: 16px;
  color: #999;
  font-size: 12px;
}

.footer {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: white;
  padding: 12px;
  display: flex;
  align-items: center;
  box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.1);
}

.price {
  flex: 1;
  color: #ff4444;
  font-size: 18px;
  font-weight: bold;
  padding-left: 16px;
}

:deep(.van-nav-bar) {
  background-color: var(--van-primary-color);
  color: white;
}

:deep(.van-nav-bar__title) {
  color: white;
}

:deep(.van-nav-bar .van-icon) {
  color: white;
}
</style>